<template>
	<view>
		<view class="search-view">
			<u-search bgColor="#fff" @change="changeTit" :showAction="false" placeholder="输入视频名称搜索"
				v-model="keyword"></u-search>
			<view class="flex align between selOption">
				<u-search bgColor="#fff" @change="changeGroup" :showAction="false" placeholder="分组名搜索"
					v-model="groupName"></u-search>
				<!-- <input placeholder="请选择分组" disabled /> -->
				<view class="optionSel flex align" style="margin-left: 30rpx;">
					<view class="flex align" @click="toUrl">添加普通视频<uni-icons type="plus" size="20"
							color="#797979"></uni-icons></view>
					<!-- <view style="margin: 0 10rpx;">|</view>
					<view class="flex align" @click="show = true">筛选<uni-icons type="settings" size="20"
							color="#797979"></uni-icons></view> -->
				</view>
			</view>
		</view>
		<view class="list" v-for="(item,index) in list" :key="index">
			<view class="name flex align between">
				<view><text v-if="item.group.name!=null">（{{item.group.name}}）</text>{{item.title}}</view>
				<view>{{item.addtime}}</view>
			</view>
			<view class="flex align between info">
				<video :src="item.video_url" controls=""></video>
				<!-- <image src="../../static/video/img.png"></image> -->
				<view class="info1">
					<view class="flex align">
						<view>视频类型</view>
						<text>{{item.type==1?'普通视频':'混剪视频'}}</text>
					</view>
					<view class="flex align">
						<view>时长（S）</view> <text>{{item.duration}}</text>
					</view>
					<view class="flex align">
						<view>大小（M）</view> <text>{{item.file_size}}</text>
					</view>
				</view>
				<view class="info2">
					<view>使用次数</view>
					<view>{{item.use_count}}</view>
					<view class="info2-btn" @click="onStartDownload(item.video_url)">保存视频</view>
				</view>
			</view>
			<view class="delBtn flex align" @click="toDel(item.id)">
				<!-- <view>修改分组</view> -->
				<view class="flex align"> <uni-icons type="trash" size="20" color="#fff"></uni-icons>
					删除</view>

			</view>
		</view>
		<xw-empty :isShow="empty" text="暂无数据" textColor="#777777"></xw-empty>
		<uni-load-more :status="status" v-if="!empty" :content-text="loadText"></uni-load-more>
		<u-popup :show="show" :round="10" mode="bottom">
			<view class="btomShow">
				<view class="closeempty" @click="show = false">
					<uni-icons type="closeempty" size="30"></uni-icons>
				</view>
				<view class="showTit">筛选</view>
				<view class="title">类型</view>
				<view class="flex align between type">
					<view>混剪视频</view>
					<view>普通视频</view>
				</view>
				<view class="title">使用次数</view>
				<view class="flex align between nums">
					<input placeholder="请输入" type="digit" />
					<view>——</view>
					<input placeholder="请输入" type="digit" />
				</view>
				<view class="title">生成时间</view>
				<view class="flex align between nums" @click="show2 = true">
					<input placeholder="请选择" v-model="range[0]" type="digit" disabled />
					<view>——</view>
					<input placeholder="请选择" v-model="range[1]" type="digit" disabled />
				</view>
			</view>
		</u-popup>
		<u-calendar :show="show2" :mode="mode" @confirm="confirm"></u-calendar>
	</view>
</template>

<script>
	import {
		tuiguangVideoIndex
	} from '@/api/apiData.js';
	export default {
		data() {
			return {
				show: false,
				show2: false,
				mode: 'range',
				range: ['', ''],
				totalPage: 1,
				page: 0,
				offset: 20,
				list: [],
				scrollTop: 0,
				empty: false,
				loadText: {
					loadmore: '点击或上拉加载更多',
					loading: '努力加载中',
					nomore: '实在没有了'
				},
				status: 'loadmore',
				keyword: '',
				groupName: ''
			};
		},
		onShow() {
			console.log(decodeURIComponent('%7B%22title%22%3A%22LIKE%22%7D'))
			this.page = 0
			this.list = []
			this.getlist();
		},
		onPageScroll(e) {
			const that = this;
			that.scrollTop = e.scrollTop;
		},
		onReachBottom() {
			const that = this;
			that.getlist();
		},
		methods: {
			toDel(id) {
				let that = this
				uni.showModal({
					title: "提示",
					content: "确定要删除吗？",
					showCancel: true,
					success: res => {
						if (res.confirm) {
							that.delCom(id)
						} else {

						}
					}

				})
			},
			delCom(id) {
				let that = this
				uni.request({
					url: 'https://jzxt.jiugezixun.cn/storeadmin/tuiguang_video/del?ids='+id,
					method: 'POST',
					header: {
						'content-type': 'application/x-www-form-urlencoded',
						'X-Requested-With': 'XMLHttpRequest',
						'Cookie': uni.getStorageSync('cookies') // 从存储中读取cookies并设置到请求头
					},
					success: result => {
						if (result.data.code == 1) {
							uni.showToast({
								title: '删除成功'
							})
							setTimeout(() => {
								that.page = 0
								that.list = []
								that.getlist()
							}, 500);
						} else {
							uni.showToast({
								title: result.data.msg,
								icon: 'none'
							})
						}
					},
					fail: result => {
						uni.hideLoading();
					}
				})
			},
			onStartDownload(url) {
				wx.getSetting({
					success: (res) => {
						if (res.authSetting['scope.writePhotosAlbum'] === undefined) {
							wx.authorize({
								scope: 'scope.writePhotosAlbum',
								success: () => {
									this.downVideo(url)
								},
								fail: (err) => {
									console.log('授权失败:', err)
								}
							})
						} else if (!res.authSetting['scope.writePhotosAlbum']) {
							wx.openSetting({
								success: (res) => {
									if (res.authSetting['scope.writePhotosAlbum']) {
										this.downVideo(url)
									}
								}
							})
						} else {
							this.downVideo(url)
						}
					}
				})
			},
			downVideo(url) {
				let that = this
				uni.showLoading({
					title: '保存中...',
				})
				uni.downloadFile({
					//视频信息的Url
					url: url,
					success: function(res) {
						// 只要服务器有响应数据，就会把响应内容写入文件并进入 success 回调，业务需要自行判断是否下载到了想要的内容
						if (res.statusCode === 200) {
							console.log(res.tempFilePath);
							uni.saveVideoToPhotosAlbum({
								filePath: res.tempFilePath,
								success: function(res) {
									console.log(res.errMsg)
									uni.hideLoading();
									uni.showToast({
										title: '已保存到相册'
									})
								},
								fail() {
									uni.showToast({
										title: '保存失败',
										icon: 'error'
									})
									uni.hideLoading();
								}
							})
						}
					},
					fail() {
						uni.showToast({
							title: '保存失败',
							icon: 'error'
						})
						uni.hideLoading();
					}
				})

			},
			changeTit(e) {
				console.log(e)
				this.page = 0
				this.list = []
				this.getlist();
			},
			changeGroup(e) {
				this.page = 0
				this.list = []
				this.getlist();
			},
			async getlist() {
				const that = this;
				const {
					totalPage,
					page,
					offset,
					list,
					group_id,
					type,
					keyword,
					groupName
				} = that;

				let obj = {
					menu_url: 'tuiguang_video',
					limit: offset,
					offset: page,
					filter: {},
					op: {}
					// op:encodeURIComponent(JSON.stringify(filter))
				};
				if (keyword != '') {
					if (groupName == '') {
						obj.filter = JSON.stringify({
							"title": keyword
						})
						obj.op = JSON.stringify({
							"title": "LIKE"
						})
					} else {
						obj.filter = JSON.stringify({
							"title": keyword,
							'group.name': groupName
						})
						obj.op = JSON.stringify({
							"title": "LIKE",
							'group.name': '='
						})
					}
				} else {
					if (groupName == '') {
						obj.filter = {}
						obj.op = {}
					} else {
						obj.filter = JSON.stringify({
							'group.name': groupName
						})
						obj.op = JSON.stringify({
							'group.name': '='
						})
					}
				}
				that.status = 'loading';
				if (totalPage > page) {
					let result = await tuiguangVideoIndex(obj);
					that.page = page + 1;
					if (result.total == 0) {
						that.empty = true;
						that.status = 'nomore';
						return;
					}
					that.empty = false;
					setTimeout(() => {
						that.status = 'loadmore';
					}, 500);
					that.list = list.concat(result.rows);
					that.totalPage = Math.ceil(result.total / offset);
				} else {
					setTimeout(() => {
						that.status = 'nomore';
					}, 500);
				}
			},
			confirm(e) {
				console.log(e)
				this.range = e
				this.show2 = false
			},
			toUrl() {
				uni.navigateTo({
					url: '/pages/videoProdue/addVideo'
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.search-view {
		padding: 20rpx 30rpx;

		.selOption {
			margin-top: 13rpx;

			input {
				background: white;
				border-radius: 23rpx;
				font-size: 24rpx;
				padding: 10rpx 20rpx;
			}

			.optionSel {
				color: #797979;
				font-size: 24rpx;
			}
		}
	}

	.list {
		background: white;
		margin: 30rpx;
		border-radius: 12rpx;
		box-shadow: 1rpx 1rpx 8rpx 2rpx gainsboro;

		.name {
			padding: 27rpx 8rpx 8rpx;
			margin: 0 17rpx;
			border-bottom: solid 1rpx #D6D6D6;

			view:nth-child(1) {
				font-size: 28rpx;
				font-weight: bold;
			}

			view:nth-child(2) {
				font-size: 18rpx;
				color: #868686;
			}
		}

		.info {
			padding: 19rpx 17rpx;

			image,
			video {
				width: 210rpx;
				height: 117.54rpx;
			}

			.info1 {
				font-size: 20rpx;

				.flex {
					margin-bottom: 6rpx;

					view {
						width: 120rpx;
					}
				}

				text {
					font-weight: bold;
				}
			}

			.info2 {
				text-align: center;
				padding: 23rpx 33rpx;
				border-left: solid 1rpx #D6D6D6;

				view:nth-child(1) {
					color: #868686;
					font-size: 18rpx;
				}

				view:nth-child(2) {
					font-size: 32rpx;
					font-weight: bold;
				}

				.info2-btn {
					background: black;
					color: white;
					font-size: 20rpx;
					padding: 5rpx 10rpx;
					border-radius: 10rpx;
					margin-top: 10rpx;
				}
			}

		}

		.delBtn {
			justify-content: center;
			background: #2D2D2D;
			border-radius: 0 0 15rpx 15rpx;
			color: white;
			font-weight: bold;
			padding: 12rpx 0;
		}
	}

	.showTit {
		text-align: center;
		font-size: 32rpx;
		font-weight: bold;
		padding: 33rpx 0;
	}

	.btomShow {
		padding: 0 28rpx 150rpx;
		position: relative;

		.closeempty {
			position: absolute;
			top: 20rpx;
			right: 30rpx;

		}

		.title {
			font-size: 28rpx;
			margin: 25rpx 0;
		}

		.type {
			view {
				background: #F7F7F9;
				width: 313rpx;
				line-height: 61rpx;
				border: solid 1rpx #E7E7E7;
				font-size: 28rpx;
				text-align: center;
				border-radius: 10rpx;
			}
		}

		.nums {
			input {
				width: 290rpx;
				height: 61rpx;
				background: #F7F7F9;
				border: solid 1rpx #E7E7E7;
				border-radius: 10rpx;
				text-align: center;
				font-size: 27rpx;
			}
		}
	}
</style>